<?php
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\StorecatModel as cat;
use think\Db;

class BrandController extends AdminBaseController
{
    /*
     * 2018 0511  再添加店铺时候直接给配套一套商品
     * */
    public function add(){
        $goods_list = Db::name('Goods')->where(array('g_status'=>1))->select()->toarray();
        $check=Db::name('StoreType')->where(array('headoffice'=>0))->count();        
        $this->assign("check", $check);
        $this->assign("goods_list", $goods_list);
        return $this->fetch();
    }
    /*
     * 2018/05/22 王磊  添加店铺类型时候直接在商品模板中添加商品
     * */
    public function addPost(){
        $cat=new cat();
        $res  = $this->request->param();
        $data=array(
            'franchise_fee'=>$res['franchise_fee'],
            'cat_name'=>$res['cat_name'],
            'headoffice'=>$res['cat'],
        );
        $re=Db::name('StoreType')->insertGetId($data);
        $state=$cat->goods_table($re);
        if($re){
            $this->success('添加成功！','brand/index');
        }else{
            $this->success('添加失败！','brand/add');
        }
    }
    public function edit(){
        $id  = $this->request->param('id');
        $info = Db::name('StoreType')->where(array('id'=>$id))->find();
        $goods_list = Db::name('Goods')->where(array('g_status'=>1))->select()->toarray();
        $this->assign("goods_list", $goods_list);
        $this->assign("info", $info);
        return $this->fetch();
    }     
    public function editPost(){
        $res  = $this->request->param();
        $data=array(
            'franchise_fee'=>$res['franchise_fee'],
            'cat_name'=>$res['cat_name'],
            'headoffice'=>$res['cat'],
        );
        $re=Db::name('StoreType')->where(array('id'=>$res['id']))->update($data);
        if($re){
            $this->success('编辑成功！','brand/index');
        }else{
            $this->success('编辑失败！','brand/add');
        }
    }
    /*
     * 品项管理
     * */
    public function brand(){
        $cat=new cat();
        $brand=$cat->brand();
        $this->assign('brand',$brand);
        return $this->fetch('brand/brand');
    }
    /*
     * 添加
     * */
    public function addbrand(){
        $cat=new cat();
        $data=$this->request->post();
        if($data !=null)
        {
           $data['ctime']=time();
           $data['utime']=time();
           $satet=DB::name('brand')->insert($data);
           if($satet){
               return $this->success('添加成功',url('brand/brand'));
           }else{
             return $this->error('添加失败',url('brand/addbrand'));
           }
        }else{

            return $this->fetch('brand/addbrand');
        }

    }
    /*
     * 删除
     * */
    public  function branddel()
    {
          $id=$this->request->param();
          $status=DB::name('brand')->where($id)->delete();
        if($status){
            return $this->success('删除成功',url('brand/brand'));
        }else{
            return $this->error('删除失败',url('brand/brand'));
        }
    }
    /*
     * 更新
     * */
    public  function updatebrand()
    {
        $id=$this->request->param();
        $data=$this->request->post();
        if($data)
        {
            $where['id']=$data['id'];
            $state=DB::name('brand')->where($where)->update($data);
            if($state)
            {
                return $this->success('更新成功',url('brand/brand'));
            }else{
                return $this->error('更新失败',url('brand/updatebrand'));
            }
        }else{
            $status=DB::name('brand')->where($id)->find();
            $this->assign('date',$status);
            return $this->fetch('brand/update');
        }
    }
}
